package model;

import util.Hash;

import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Objects;

/**
 * PackageName: model
 * ClassName: FileContent
 * Author: tum0r
 * Time: 2020/5/23 16:14
 */
public class FileContent extends BaseModel {
    private byte[] Data;

    public byte[] getData() {
        return Data;
    }

    public void setData(byte[] data) {
        Data = Objects.requireNonNullElseGet(data, () -> new byte[0]);
    }

    public FileContent(String UUID, byte[] data, String fingerprint, double size) {
        setUUID(UUID);
        setData(data);
        setFingerprint(fingerprint);
        setSize(size);
    }

    public FileContent() {
        super();
        setData(null);
    }

    public void setFingerPrintByContent() {
        try {
            Hash hash = new Hash();
            setFingerprint(hash.getHash(getData()));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String toString() {
        return "FileContent{" +
                "Data=" + Arrays.toString(Data) +
                ", Fingerprint='" + Fingerprint + '\'' +
                ", UUID='" + UUID + '\'' +
                ", Size=" + Size +
                ", Unit='" + Unit + '\'' +
                '}';
    }
}
